package cn.example.demo.modules.sys.controller;

import cn.example.demo.common.config.AliyunConfig;
import cn.example.demo.common.model.response.HttpDataResponseResult;
import cn.example.demo.common.model.response.HttpResponseResult;
import cn.example.demo.common.secure.annotation.AuthEnable;
import cn.example.demo.common.tools.file.ExportFileUtils;
import cn.example.demo.common.tools.obj.DateAgeUtils;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSSClient;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 文件上传控制器
 * </p>
 *
 * @author Lizuxian
 * @create 2022/3/6 18:56
 */
@RestController
@RequestMapping("/api/file/upload")
public class FileUploadController {
    @Autowired
    private OSSClient ossClient;

    @Autowired
    private AliyunConfig aliyunConfig;

    @AuthEnable
    @PostMapping(value = "image", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "图片上传")
    public HttpResponseResult uploadImage(@ApiParam(value = "图片") @RequestParam(value = "file") MultipartFile file) throws IOException {
        if (file == null) {
            return HttpDataResponseResult.isBadRequest("请求参数有误，图片不能为空！", null);
        }

        String extension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String fileName = UUID.randomUUID() + extension;

        String month = DateAgeUtils.dateToString(new Date(), "yyyy-MM");
       // String filePath = StrUtil.format("image/", File.separator, month, File.separator, fileName);
        //创建文件新的路径
        String filePath = "image/" + month + "/" + fileName;
        System.out.println(filePath);
        // 上传到阿里云
        try {
            // 目录结构：images/2018/12/29/xxxx.jpg
            ossClient.putObject(aliyunConfig.getBucketName(), filePath, new ByteArrayInputStream(file.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
            return HttpDataResponseResult.isBadRequest("图片上传失败！", null);
        }
        // ExportFileUtils.writeToFile(file.getBytes(), "resource.file" + File.separator + filePath);

        return HttpDataResponseResult.isSuccess(filePath);
    }

    @AuthEnable
    @PostMapping(value = "document", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "文档上传")
    public HttpResponseResult uploadDocument(@ApiParam(value = "文档") @RequestParam(value = "file") MultipartFile file) throws IOException {
        if (file == null) {
            return HttpDataResponseResult.isBadRequest("请求参数有误，文档不能为空！", null);
        }

        String month = DateAgeUtils.dateToString(new Date(), "yyyy-MM");
        String filePath = StrUtil.format("document{}{}{}{}", File.separator, month, File.separator, file.getOriginalFilename());

        ExportFileUtils.writeToFile(file.getBytes(), "resource.file" + File.separator + filePath);

        return HttpDataResponseResult.isSuccess(filePath);
    }
}
